﻿using System;
using System.Collections.Generic;
using System.Data;
using visinia.SmartEngine.Cache;
using viWiki.Classes;
using viWiki.Providers;

namespace viWiki.Controllers
{
    public class WikiPageLinkController
    {
        #region WIKI PAGE LINKS
        private static List<WikiPageLink> GetWikiPageLnksInternal()
        {
            List<WikiPageLink> list = (List<WikiPageLink>)DataCache.GetCache("WikiPageLinks");
            if (list == null)
            {
                IDataReader dr = viWikiDataProvider.Provider.GetWikiPageLinks();
                list = new List<WikiPageLink>();                
                while (dr.Read())
                {
                    WikiPageLink link = new WikiPageLink();
                    if (dr["SourceWikiPageId"] != Convert.DBNull)
                        link.SourceWikiPageId = (int)dr["SourceWikiPageId"];
                    if (dr["DestinationWikiPageId"] != Convert.DBNull)
                        link.DestinationWikiPageId = (int)dr["DestinationWikiPageId"];
                    list.Add(link);
                }
                dr.Close();
                DataCache.SetCache("WikiPageLinks", list);
            }
            return list;
        }

        public static List<WikiPageLink> GetWikiPageLinks(int DestinationWikiPageId)
        {
            List<WikiPageLink> links = GetWikiPageLnksInternal().FindAll(p => p.DestinationWikiPageId == DestinationWikiPageId);
            return links;
        }       

        public static void AddWikiPageLinks(int SourceWikiPageId, List<int> DestinationWikiPageIds)
        {
            //reset all outgoing links
            DeleteWikiPageLinks(SourceWikiPageId);
            // generate add links
            foreach (int DestinationWikiPageId in DestinationWikiPageIds)
            {
                if (SourceWikiPageId == DestinationWikiPageId)
                    continue;
                if (WikiPageController.GetWikiPage(DestinationWikiPageId) != null)
                    viWikiDataProvider.Provider.AddWikiPageLink(SourceWikiPageId, DestinationWikiPageId);
            }
            DataCache.DeleteCacheMultiple("WikiPageLinks");
        }

        public static void DeleteWikiPageLinks(int SourceWikiPageId)
        {
            viWikiDataProvider.Provider.DeleteWikiPageLinks(SourceWikiPageId);
            DataCache.DeleteCacheMultiple("WikiPageLinks");
        }

        public static List<WikiPage> GetOrphanWikiPages()
        {
            List<WikiPage> orphanWikiPages = (List<WikiPage>)DataCache.GetCache("WikiPageLinksOrphan");
            if (orphanWikiPages == null)
            {
                orphanWikiPages = new List<WikiPage>();
                List<NamespaceInfo> namespaces = NamespaceController.GetNamespaces();
                foreach (NamespaceInfo nspace in namespaces)
                {
                    List<WikiPage> wikipages = WikiPageController.GetWikiPagesByNamespace(nspace.NamespaceInfoId);
                    foreach (WikiPage wikipage in wikipages)
                    {
                        if (wikipage.WikiPageId == WikiController.WikiRootPageId)
                            continue;
                        List<WikiPageLink> incomminglinks = GetWikiPageLinks(wikipage.WikiPageId);
                        if ((incomminglinks == null) | (incomminglinks.Count == 0))
                            orphanWikiPages.Add(wikipage);
                    }
                }
                DataCache.SetCache("WikiPageLinksOrphan", orphanWikiPages);
            }
            return orphanWikiPages;
        }
        #endregion
    }
}
